package org.honeyweb.gsoc.pages;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Secure;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.internal.services.LinkImpl;
import org.apache.tapestry5.internal.services.RequestPathOptimizer;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.honeyweb.gsoc.model.Users;
import org.honeyweb.gsoc.services.UsersManager;

@SuppressWarnings("unused")
public class Connect {

	private final Log logger = LogFactory.getLog(Connect.class);

	private static final String BAD_CREDENTIALS = "Bad login and/or password. Please retry.";

	@Persist
	private boolean error;

	@Inject
	private Request request;

	@Inject
	private Response response;

	@Component
	private Form form;

	@SessionState(create = false)
	private String userName;

	@SessionState
	private Users user;

	@Property
	private String password;

	private RequestPathOptimizer optimizer;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getErrorMessage() {
		String ret = null;
		if (error) {
			ret = BAD_CREDENTIALS;
		}
		return ret;
	}

	Link onSuccess() {
		Link link = new LinkImpl("j_acegi_security_check", false, false,
				response, optimizer);
		link.addParameter("j_username", userName);
		link.addParameter("j_password", password);
		return link;

	};

	public void onActivate() {
		error = new Boolean(request.getParameter("error")).booleanValue();
	}

}
